package com.jike.noobmoney.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.lang.reflect.Method;
import java.util.List;

import static android.content.Context.TELEPHONY_SERVICE;

/**
 * 获取设备信息
 */
public class AppInfoUtils {

    private static TelephonyManager telephonyManager;

    /**
     * @param context
     * @return
     */
    private static TelephonyManager getTelephonyManager(Context context) {
        if (telephonyManager == null) {
            telephonyManager = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
        }
        return telephonyManager;
    }

    /**
     * 获取手机的imei
     */
    public static String getPhoneIMIE(Context context) {
        String imei = getTelephonyManager(context).getDeviceId();
        return imei;
    }


    /**
     * 设备序列号
     *
     * @return
     */
    public static String getSerialNumber(Context context) {
        String serial = "";
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            serial = (String) get.invoke(c, "ro.serialno");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(serial)) {
            //serial = getTelephonyManager(context).getSimSerialNumber();
            serial = android.os.Build.SERIAL;
        }
        return serial;
    }

    /**
     * 获取手机系统
     *
     * @return
     */
    public static String getPhoneInfo() {
        try {
            String brand = Build.BRAND; // 手机品牌
            String model = Build.MODEL; // 型号
            String device = Build.VERSION.RELEASE; // 系统版本
            return brand + model + "-" + device;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String getBrand() {
        try {
            String brand = Build.BRAND; // 手机品牌
            return brand;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getModel() {
        try {
            String model = Build.MODEL; // 手机型号
            return model;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getOsVersion() {
        try {
            String device = Build.VERSION.RELEASE; // 系统版本
            return device;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取版本信息
     *
     * @param context
     * @return
     */
    public static int getPackageVersionCode(Context context) {
        PackageInfo packageInfo = null;// 版本号
        try {
            packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取手机内安装的应用列表
     *
     * @param context
     * @return
     */
    public static List<PackageInfo> getPhoneAppList(Context context) {
        PackageManager pm = context.getPackageManager();

        List<PackageInfo> list = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);

        return list;

    }

    /**
     * 获取本地版本名称
     **/
    public static String getAppVersion(Context context) {
        if (null == context) {
            return "0";
        }
        String codeName = null;
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            codeName = info.versionName;
            return codeName;
        } catch (PackageManager.NameNotFoundException e) {
        }
        return null;
    }

    /**
     * 获取本地版本名称
     **/
    public static int getAppVersionCode(Context context) {
        if (null == context) {
            return 0;
        }
        int codeName = 0;
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            codeName = info.versionCode;// 版本名称1.1.1
            return codeName;
        } catch (PackageManager.NameNotFoundException e) {
        }
        return 0;
    }

    /**
     * 获取AndroidID
     *
     * @param context
     * @return
     */
    public static String getAndroidID(Context context) {
        String androidID = android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
        return androidID;
    }

}

